package com.inspur.demo.web;

import com.inspur.demo.core.result.Result;
import com.inspur.demo.model.User;
import com.inspur.demo.model.User2;
import com.inspur.demo.service.IUser2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName UserController
 * @Description TODO
 * @Company inspur
 * @Author Mark.Zhang
 * @Date 2019/5/16 10:18
 * @Version 1.0 
 */
@RestController
@Api(value = "书籍管理类", description = "管理书籍的增删改查")
@CacheConfig(cacheNames = "userCache")
public class User2Controller {
    @Autowired
    IUser2Service user2Service;

    @RequestMapping(value = "/createBook")
    @ApiOperation(value = "书籍新增", notes = "根据书名和数量创建新书")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bookname", value = "书名", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "count", value = "数量", paramType = "query", dataType = "int", required = true)
    })
    public User2 create(String bookname, int count) {
        User2 user = new User2(bookname, count, new Date());
        User2 u = user2Service.create(user);
        return u;
    }

    @RequestMapping(value = "/findBook/{id}")
    @ApiOperation(value = "书籍查询", notes = "根据ID查询书籍")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "书籍ID", paramType = "path", dataType = "String", required = true)
    })
    @Cacheable(key = "#id")
    public Result create(@PathVariable String id) {
        User2 u = user2Service.find(id);
        return Result.success(u);
    }

}
